package com.mentaleye.controller;

import java.util.Date;
import java.util.List;

import com.mentaleye.biz.WorkTimeBiz;
import com.mentaleye.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.SpecialtimeBean;
import com.mentaleye.biz.SpecialtimeBiz;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping(value = "/specialtime")
public class SpecialtimeController {
   
	@Autowired private  SpecialtimeBiz specialtimeBiz;
	@Autowired private  WorkTimeBiz workTimeBiz;
	@RequestMapping(value = "SpworktimeList")
	public ModelAndView list(@RequestParam(value="worktimeNo",required=false) Integer worktimeNo) {
        ModelAndView view = new ModelAndView("ss/specialtime/SpworktimeList");
        System.out.println("worktimeNo"+"------>"+worktimeNo);
        view.addObject("worktimeNo",worktimeNo);
		view.addObject("worktimeBean",workTimeBiz.queryWorktimeDate(worktimeNo));
        return view;
    }
	@RequestMapping(value="/specialtimeData")
	  @ResponseBody
	   public String specialtimeData(CommonPage commonPage){
		specialtimeBiz.SpWorktimeByPagequery(commonPage);
		  
		  return commonPage.toSHowPage();
	  }
	@RequestMapping(value = "/delete")
	public  @ResponseBody String delete(@RequestBody List<Integer> ids){
		  return JSONObject.toJSONString(specialtimeBiz.Specialtimedelete(ids));
		  
	  }
	@RequestMapping(value="/sava")
	  public @ResponseBody String sava(  SpecialtimeBean specialtimeBean,@RequestParam(value = "startTime")String startTime,
										 @RequestParam(value = "finTime")String finTime){
		Date beginTime = DateUtils.getDateByStr(startTime, "yyyy-MM-dd HH:mm");
		Date endTime = DateUtils.getDateByStr(finTime, "yyyy-MM-dd HH:mm");
		specialtimeBean.setBeginTime(beginTime);
		specialtimeBean.setEndTime(endTime);
		long day=(specialtimeBean.getEndTime().getTime()-specialtimeBean.getBeginTime().getTime())/86400000;
		double hour=((double)((specialtimeBean.getEndTime().getTime()-specialtimeBean.getBeginTime().getTime())%86400000)/3600000)/12;
		if(hour>0.5){
			hour=1.0;
		}else{
			hour=0.0;
		}
		double sum=(double)day+hour;
		System.out.println(sum);
		specialtimeBean.setMuchDay(sum);
		return JSONObject.toJSONString(specialtimeBiz.SpecialtimeEdit(specialtimeBean));
	  }
	 @RequestMapping(value="/SpecialtimeEdit")
	  public ModelAndView SpecialtimeEdit(@RequestParam(value="swId",required=false) Integer swId,@RequestParam(value="worktimeNo",required=false) Integer worktimeNo){
	  	ModelAndView view=new ModelAndView("ss/specialtime/SpecialtimeEdit");
		 System.out.println(worktimeNo+"+++++++++++++++++++++++++++++++++++++"+swId);
		  view.addObject("worktimeNo", worktimeNo);
		  if(swId!=null){
			  SpecialtimeBean a=specialtimeBiz.Spworktimequery(swId);
		   view.addObject("specialtime", specialtimeBiz.Spworktimequery(swId));
		  }
		  return view;
	  }
}
